<html>
<head><meta charset="utf-8"><title>upgrade unreachable_patterns to deny by default · edition 2021 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/index.html">edition 2021</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html">upgrade unreachable_patterns to deny by default</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221340201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221340201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221340201">(Jan 01 2021 at 07:24)</a>:</h4>
<p>Would it be possible to upgrade the <code>unreachable_patterns</code> lint to be deny by default in the next edition? It's nearly always incorrect to have an unreachable_pattern. Usually when it happens it's on accident because you match on a constant that you forgot to import, so <code>MY_CONST</code> ends up being a catch-all case.</p>



<a name="221345323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221345323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221345323">(Jan 01 2021 at 10:03)</a>:</h4>
<p>I think it needs to be a more nuanced lint, to get upgraded.  For example, this is <code>unreachable_patterns</code> now, but didn't used to be:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="mi">5_</span><span class="k">u8</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="mi">0</span><span class="o">..=</span><span class="mi">255</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I'd love to get a new lint that we could make deny-by-default.  We do often trigger three different lints, so maybe there's a combination of those we could look for specifically -- I don't think that any of <code>unreachable_patterns</code>+<code>unused_variables</code>+<code>non_snake_case</code> individually deserve to be deny-by-default, but when a match triggers all three, it probably ought to be deny.</p>



<a name="221347197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221347197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221347197">(Jan 01 2021 at 11:03)</a>:</h4>
<p>FWIW, I'll throw in catch-all patterns in the middle of a match sometimes if I want to do a quick test that a certain input value doesn't trigger early match values, including printing the value, so while I like it as a warning, I'd rather it not be a hard error.</p>



<a name="221347272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221347272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221347272">(Jan 01 2021 at 11:05)</a>:</h4>
<p>Or if I have a bunch of unimplemented patterns and I can just return a dummy value. Or if I have rust-analyzer give me all the patterns, and then I condense them; I'll clean up the unused patterns after I've got all the patterns I do want.</p>



<a name="221365236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221365236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221365236">(Jan 01 2021 at 19:32)</a>:</h4>
<p>Are there guidelines somewhere on what should or shouldn't be deny-by-default? My mental model is that errors should indicate something that's wrong enough that it better be fixed before code can run. That's not the case for unreachable patterns.</p>



<a name="221365456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221365456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221365456">(Jan 01 2021 at 19:38)</a>:</h4>
<p>Also unreachable patterns feels similar to dead_code and various "unused" lints. When I refactor those keep popping up; I would be severely annoyed if I couldn't run tests mid-refactor because some code is unreachable. I experienced this because the rustc codebase sets deny-warnings=true by default; I found it painful.</p>



<a name="221365535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221365535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221365535">(Jan 01 2021 at 19:40)</a>:</h4>
<p>(cc <a href="https://github.com/rust-lang/rust/pull/77492">https://github.com/rust-lang/rust/pull/77492</a>)</p>



<a name="221365613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221365613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221365613">(Jan 01 2021 at 19:42)</a>:</h4>
<p>Alright, well, i refine the proposal to what Scott said, because i don't mean to catch mid-refactor unreachables, but I would like to stop "you named a const but it wasn't imported (or it was a typo) and now it's a catch-all case on accident."</p>



<a name="221365744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221365744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221365744">(Jan 01 2021 at 19:46)</a>:</h4>
<p>Oh I see, that's a problematic case indeed. I like Scott's proposal</p>



<a name="221366481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221366481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221366481">(Jan 01 2021 at 20:05)</a>:</h4>
<p>I think that the issue with <code>const</code>ants can be handled in its vast majority by denying all-caps catch-up bindings that are not followed by an explicit <code>@ _</code> (and this is not <code>nonstandard_style</code> (nor <code>non_snake_case</code>):<code>nonstandard_style</code> is an opinionated lint that just happens to be one of the closest things we have to this suggestion). While oddly specific, in practice, such a lint would be surprisingly effective, which is kind of what we look for when dealing with lints. While not necessarily a deny-by-default lint, it would already allow using great diagnostics, and those who want it could opt into denying such specific lint (this whole thing makes me think such a lint should start in clippy <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span>)</p>



<a name="221368842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221368842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221368842">(Jan 01 2021 at 21:05)</a>:</h4>
<p>I agree that looking for suspiciously named variables is better than using <code>unreachable_patterns</code> to catch this, because <code>unreachable_patterns</code> only fires on the <em>second</em> unknown-constant-reinterpreted-as-variable, which means that if there is only one then you just have silent breakage</p>



<a name="221369061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221369061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221369061">(Jan 01 2021 at 21:10)</a>:</h4>
<p>for example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">mod</span> <span class="nn">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">BAR</span>: <span class="kt">u8</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">assert!</span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">BAR</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p><code>unreachable_patterns</code> doesn't notice this, although the mentioned style lints fire here since <code>BAR</code> is not a good variable name</p>



<a name="221372207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372207">(Jan 01 2021 at 22:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245339">Nadrieril</span> <a href="#narrow/stream/268952-edition/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default/near/221365236">said</a>:</p>
<blockquote>
<p>Are there guidelines somewhere on what should or shouldn't be deny-by-default? My mental model is that errors should indicate something that's wrong enough that it better be fixed before code can run. That's not the case for unreachable patterns.</p>
</blockquote>
<p>I don't think there's an official one; my personal one is (<a href="https://github.com/rust-lang/rfcs/pull/2438#issuecomment-403255258">https://github.com/rust-lang/rfcs/pull/2438#issuecomment-403255258</a>):</p>
<blockquote>
<p>Is this something that's going to make what I'm trying right now not work?</p>
</blockquote>
<p>Which is essentially the same as the "is it worth running my unit tests" you mentioned.</p>
<p>For example, there's no way that this is actually what I wanted, even temporarily, so deny is appropriate:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">std</span>::<span class="n">net</span>::<span class="n">IpAddr</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="nb">static</span> <span class="kt">str</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">V4</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="s">"version 4"</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">V6</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="s">"version 6"</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221372395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372395">(Jan 01 2021 at 22:35)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/268952-edition/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default/near/221365613">said</a>:</p>
<blockquote>
<p>Alright, well, i refine the proposal to what Scott said, because i don't mean to catch mid-refactor unreachables, but I would like to stop "you named a const but it wasn't imported (or it was a typo) and now it's a catch-all case on accident."</p>
</blockquote>
<p>I think logistically the answer here is to make a new focused lint, and then we can start turning it on in <em>all</em> editions (assuming it's correctly focused enough that the code it breaks in crater is really incorrect).</p>
<p>Want to sign up to make that lint, <span class="user-mention" data-user-id="224471">@Lokathor</span>?</p>



<a name="221372444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372444">(Jan 01 2021 at 22:36)</a>:</h4>
<p>I would prefer deny-by-default because, when testing, half the time RA doesn't show warnings, and the other half the time there's dozens of warnings for hours at a time, so "cargo test" or "cargo run" should just fail else the warning will get lost in the shuffle</p>



<a name="221372449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372449">(Jan 01 2021 at 22:36)</a>:</h4>
<p>also, sorry scott, but i don't really contribute to the <del>standard library</del> compiler, it's too much of a pain</p>



<a name="221372454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372454">(Jan 01 2021 at 22:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/268952-edition/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default/near/221372449">said</a>:</p>
<blockquote>
<p>also, sorry scott, but i don't really contribute to the standard library, it's too much of a pain</p>
</blockquote>
<p>happy to help with this btw</p>



<a name="221372456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372456">(Jan 01 2021 at 22:37)</a>:</h4>
<p>Yes, turn it on as deny-by-default in all editions.</p>
<p>(We don't <em>need</em> an edition to make something deny-by-default, thanks to cap-lints and such.)</p>



<a name="221372461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372461">(Jan 01 2021 at 22:37)</a>:</h4>
<p>have you tried it since the bootstrapping changes?</p>



<a name="221372656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221372656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221372656">(Jan 01 2021 at 22:42)</a>:</h4>
<p>No, I just work on projects that actually work properly with <code>cargo</code>, and the rest of the time i practice the art of shouting loudly.</p>



<a name="221399277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/upgrade%20unreachable_patterns%20to%20deny%20by%20default/near/221399277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/upgrade.20unreachable_patterns.20to.20deny.20by.20default.html#221399277">(Jan 02 2021 at 13:04)</a>:</h4>
<p>It's not <em>that</em> bad^^ actually understanding rustc internals is a lot more complicated than managing x.py...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>